package com.wcq.studentbackend.controller;

import com.wcq.studentbackend.common.R;
import com.wcq.studentbackend.dto.DashboardStatsDTO;
import com.wcq.studentbackend.dto.ClassStudentCountDTO;
import com.wcq.studentbackend.dto.ExamAverageScoreDTO;
import com.wcq.studentbackend.mapper.ClassMapper;
import com.wcq.studentbackend.mapper.GradeMapper;
import com.wcq.studentbackend.service.ClassService;
import com.wcq.studentbackend.service.ExamService;
import com.wcq.studentbackend.service.StudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
@Api(value = "Dashboard管理")
@Slf4j
@RestController
@RequestMapping("/api/dashboard") // 或者 /api/admin/dashboard
public class DashboardController {

    @Autowired
    private StudentService studentService;

    @Autowired
    private ClassService classService;

    @Autowired
    private ExamService examService;

    @Autowired
    private ClassMapper classMapper;

    @Autowired
    private GradeMapper gradeMapper;
    @ApiOperation(value = "获取仪表盘统计数据接口")
    @GetMapping("/stats")
    public R<DashboardStatsDTO> getDashboardStats() {
        log.info("Fetching dashboard statistics...");
        try {
            long totalStudents = studentService.count();
            long totalClasses = classService.count();
            long totalExams = examService.count();

            DashboardStatsDTO stats = new DashboardStatsDTO();
            stats.setTotalStudents(totalStudents);
            stats.setTotalClasses(totalClasses);
            stats.setTotalExams(totalExams);

            log.info("Dashboard stats: Students={}, Classes={}, Exams={}", totalStudents, totalClasses, totalExams);
            return R.success(stats);
        } catch (Exception e) {
            log.error("Error fetching dashboard statistics", e);
            return R.error("获取仪表盘统计数据失败");
        }
    }
    @ApiOperation(value = "获取班级学生数量统计接口")
    @GetMapping("/class-student-counts")
    public R<List<ClassStudentCountDTO>> getClassStudentCounts() {
        log.info("Fetching class student counts for dashboard...");
        try {
            List<ClassStudentCountDTO> counts = classMapper.getClassStudentCounts();
            log.info("Found {} class student count entries.", counts.size());
            return R.success(counts);
        } catch (Exception e) {
            log.error("Error fetching class student counts", e);
            return R.error("获取班级学生数量统计失败");
        }
    }
    @ApiOperation(value = "获取考试平均分统计接口")
    @GetMapping("/exam-average-scores")
    public R<List<ExamAverageScoreDTO>> getExamAverageScores() {
        log.info("Fetching exam average scores for dashboard...");
        try {
            List<ExamAverageScoreDTO> scores = gradeMapper.getRecentExamAverageScores();
            // 注意：averageScore 可能为 null 如果某个考试没有成绩，前端需要处理
            log.info("Found {} exam average score entries.", scores.size());
            return R.success(scores);
        } catch (Exception e) {
            log.error("Error fetching exam average scores", e);
            return R.error("获取考试平均分统计失败");
        }
    }
} 